//
//  XiuVisitViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/2.
//  Copyright © 2017年 金橙云. All rights reserved.
//
#define IS_iOS8 [[UIDevice currentDevice].systemVersion floatValue] >= 8.0f
#define IS_iOS6 [[UIDevice currentDevice].systemVersion floatValue] >= 6.0f


#import "XiuVisitViewController.h"
#import "UserfulOneCell.h"
#import "UserfulTwoCell.h"
#import "UserfulThreeCell.h"
#import "UserfulForeCell.h"
#import "UserfulFiveCell.h"
//
#import "AddMModel.h"



#import "TodoHttpManager.h"
#import "FTPopOverMenu.h"
#import "KangYinSuVc.h"
#import "ClientViewController.h"
#import "JobViewController.h"

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import "CellThreeView.h"

#import "XiuPhoneView.h"
@interface XiuVisitViewController ()<UITableViewDelegate,UITableViewDataSource,UserfulForeCellDelegate,UITextFieldDelegate,UserfulThreeCellDelegate,UserfulFiveCellDelegate,UserfulOneCellDelegate,ABPeoplePickerNavigationControllerDelegate,CellThreeViewDelegate>
{
    NSMutableArray *tfArr;
    NSMutableArray *oneArr;
    NSMutableDictionary *zongDic;
    NSMutableArray *egeArr;
    NSMutableArray *egeJArr;
    NSMutableArray *dengArr;
    NSMutableArray *dengJArr;
    NSMutableArray *_oneArr;
    NSMutableArray *_bArr;
    NSMutableArray *_cArr;
    NSMutableArray *_dArr;
    NSMutableArray *_eArr;
    NSMutableArray *_fArr;
    NSMutableArray *_gArr;
    NSMutableArray *_hArr;
    NSMutableArray *_iArr;
    NSMutableArray *_JArr;
    NSMutableArray *_kArr;
    
    NSString *_timeStr;//来访日期
    NSString *_keNameStr;//客户姓名
    NSString *_lianStr;//联系方式
    NSString *_sexStr;//性别
    NSString *_IDstr;//身份证号码
    NSString *_IDAddStr;//身份证地址
    NSString *_keDengStr;//客户等级
    NSString *_ageStr;
    NSString *_zhuanStr;
    NSString *_adStr;
    NSString *_tQStr;
    NSString *_tFStr;
    
    NSString *_tNameStr;
    NSString *_tPhoneStr;
    NSString *_liveStr;
    NSString *_workStr;
    NSString *_zhiStr;
    NSString *_trafficStr;
    NSString *_zhiMStr;
    NSString *_xWStr;
    NSString *_xMianStr;
    NSString *_xHuStr;
    NSString *_poneStr;
    NSString *_xiaStr;
    
    
    NSString *_kangYStr;
    
    NSString *phone_A ;
    NSString *phone_B;
    
    ABPeoplePickerNavigationController *_abPeoplePickerVc;
    UIButton *_btn1;
    UIButton *_btn2;
    UIButton *_btn3;
    NSMutableArray *_exitArr;
    
    UserfulTwoCell *_threecell;
    
    BOOL _isupdatelayout;
    
    CGFloat changeheight;
    
    

}



@property(nonatomic,strong)NSMutableArray *seletedPhoneArry;
@property (nonatomic, assign, getter = isSelf_adaption) BOOL self_adaption;

@property (nonatomic, strong) NSString         *_is_string;

@property (nonatomic, strong) CellThreeView         *threeview;


//@property(nonatomic,strong)XiuPhoneView *threeview;


@property(nonatomic,strong)UITableView *tabView;


@property(nonatomic,strong)NSMutableArray *zhuanArr;
@property(nonatomic,strong)NSMutableArray *adArr;
@property(nonatomic,strong)NSMutableArray *tuiQArr;
@property(nonatomic,strong)NSMutableArray *tuiFArr;
@property(nonatomic,strong)NSMutableArray *juArr;
@property(nonatomic,strong)NSMutableArray *workArr;
@property(nonatomic,strong)NSMutableArray *IDArr;
@property(nonatomic,strong)NSMutableArray *traficArr;
@property(nonatomic,strong)NSMutableArray *muDArr;
@property(nonatomic,strong)NSMutableArray *xWuArr;
@property(nonatomic,strong)NSMutableArray *xMianArr;
@property(nonatomic,strong)NSMutableArray *xHukArr;
@property(nonatomic,strong)NSMutableArray *kangArr;//抗性因素
@property(nonatomic,strong)NSMutableArray *yinArr;
//
@property (nonatomic,strong) AddMModel *model;
@property (nonatomic,strong) DateModel *dModel;

//男女选择
@property (nonatomic,strong) NSString *sexString;



@property (nonatomic, strong) NSMutableArray         *pphoneArray;

@property (nonatomic,copy) NSString *tabString;

@property (nonatomic,assign) CGFloat kangYinHeight;
@property (nonatomic,assign) CGFloat kangYinTextHeight;

@end

@implementation XiuVisitViewController


-(NSMutableArray *)seletedPhoneArry
{
    if (!_seletedPhoneArry) {
        _seletedPhoneArry = [NSMutableArray array];
    }
    return _seletedPhoneArry;
}

-(CellThreeView *)threeview
{
    if (!_threeview)
    {
        CGFloat vHeight = 0;
        if (self.seletedPhoneArry.count>=3) {
            vHeight = 150;
        }else if (self.seletedPhoneArry.count==2){
            vHeight = 100;
        }else{
            vHeight = 50;
        }
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.seletedPhoneArry];
        _threeview = [[CellThreeView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, vHeight) witharray:arr];
        _threeview.backgroundColor = [UIColor whiteColor];
        _threeview.dataarray = arr;
        _isupdatelayout = NO;
        __weak __typeof(self) wself = self;

        _threeview.rerturnbtn = ^(CGFloat height) {
            _isupdatelayout = YES;
            changeheight = height;
            [wself changethreecellframewithheight:height];
            [wself.tabView reloadData];
        };
    }
    
    return _threeview;
}

- (void)changethreecellframewithheight:(CGFloat )height{
    CGRect frame = _threecell.frame;
    frame.size.height = height;
    _threecell.frame = frame;
    
}


-(void)viewWillAppear:(BOOL)animated
{
    //下拉菜单
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuDropDown:) name:TDGuDropDown object:nil];
    [_manager userGuWenDropDown];
    //修改来访
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuKeXLaiFX:) name:TDGuKeXLaiFX object:nil];
    
    
}
//下拉菜单
-(void)didTDGuDropDown:(NSNotification *)noti
{
    if (noti.object != 0)
    {
        zongDic = [noti.object objectForKey:@"data"];
        //客户等级
        dengArr = [zongDic objectForKey:@"khdj"];
        for (int i = 0; i < dengArr.count; i++)
        {
            NSDictionary *dict = [dengArr objectAtIndex:i];
            [dengJArr addObject:dict[@"content"]];
        }
        //年龄阶段
        egeArr = [zongDic objectForKey:@"age_group"];
        for (int i = 0; i < egeArr.count; i++)
        {
            NSDictionary *dict = [egeArr objectAtIndex:i];
            [egeJArr addObject:dict[@"content"]];
            
        }
        //来电转来访
        
        
        //广告推广
        _oneArr = [zongDic objectForKey:@"ggtg"];
        // NSLog(@"广告推广========   %@",_oneArr);
        for (int i = 0; i < _oneArr.count; i++)
        {
            NSDictionary *dict = [_oneArr objectAtIndex:i];
            // NSLog(@"字典里是什么=======%@",dict);
            [self.adArr addObject:dict[@"content"]];
            // NSLog(@"self.adAr是什么=======%@",self.adArr);
        }
        
        //推介渠道
        _bArr = [zongDic objectForKey:@"tjqd"];
        for (int i = 0; i < _bArr.count; i++)
        {
            NSDictionary *dict = [_bArr objectAtIndex:i];
            [_tuiQArr addObject:dict[@"content"]];
        }
        //推介方式
        _cArr = [zongDic objectForKey:@"tjfs"];
        for (int i = 0; i < _cArr.count; i++)
        {
            NSDictionary *dict = [_cArr objectAtIndex:i];
            [_tuiFArr addObject:dict[@"content"]];
        }
        //居住区域
        _dArr = [zongDic objectForKey:@"live_area"];
        for (int i = 0; i < _dArr.count; i++)
        {
            NSDictionary *dict = [_dArr objectAtIndex:i];
            [_juArr addObject:dict[@"content"]];
        }
        //工作区域
        _eArr = [zongDic objectForKey:@"work_area"];
        for (int i = 0; i < _eArr.count; i++)
        {
            NSDictionary *dict = [_eArr objectAtIndex:i];
            [_workArr addObject:dict[@"content"]];
        }
        //职业身份
        _fArr = [zongDic objectForKey:@"job"];
        for (int i = 0; i < _fArr.count; i++)
        {
            NSDictionary *dict = [_fArr objectAtIndex:i];
            [_IDArr addObject:dict[@"content"]];
        }
        //交通工具
        _gArr = [zongDic objectForKey:@"vehicle"];
        for (int i = 0; i < _gArr.count; i++)
        {
            NSDictionary *dict = [_gArr objectAtIndex:i];
            [_traficArr addObject:dict[@"content"]];
        }
        //置业目的
        _hArr = [zongDic objectForKey:@"zymd"];
        for (int i = 0; i < _hArr.count; i++)
        {
            NSDictionary *dict = [_hArr objectAtIndex:i];
            [_muDArr addObject:dict[@"content"]];
        }
        //需求物业
        _iArr = [zongDic objectForKey:@"need_property"];
        for (int i = 0; i < _iArr.count; i++)
        {
            NSDictionary *dict = [_iArr objectAtIndex:i];
            [_xWuArr addObject:dict[@"content"]];
        }
        //需求面积
        _JArr = [zongDic objectForKey:@"order_area"];
        for (int i = 0; i < _JArr.count; i++)
        {
            NSDictionary *dict = [_JArr objectAtIndex:i];
            [_xMianArr addObject:dict[@"content"]];
        }
        //需求户型
        _kArr = [zongDic objectForKey:@"order_type"];
        for (int i = 0; i < _kArr.count; i++)
        {
            NSDictionary *dict = [_kArr objectAtIndex:i];
            [_xHukArr addObject:dict[@"content"]];
        }
        
        //抗性因素
        _yinArr = [zongDic objectForKey:@"doubt"];
        for (int i = 0; i<_yinArr.count; i++)
        {
            NSDictionary *dict = [_yinArr objectAtIndex:i];
            [_kangArr addObject:dict[@"content"]];
        }
        
        if (_kangYStr.length>0) {
            NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};  //指定字号
            CGRect rect = [_kangYStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-8-105, MAXFLOAT)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                           NSStringDrawingUsesFontLeading attributes:dic context:nil];
            _kangYinTextHeight = rect.size.height+16;
            if (_kangYinTextHeight<18) {
                _kangYinTextHeight = 18;
            }
            _kangYinHeight = _kangYinTextHeight +16*2;
        }else{
            _kangYinTextHeight = 18;
            _kangYinHeight = 50;
        }

        
        [_tabView reloadData];
    }
    
}

//修改来访

-(void)didTDGuKeXLaiFX:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int stutue = [[notifi.object objectForKey:@"status"] intValue];
        if (stutue == 1)
        {
            [_manager MakeMyselfAlert:@"修改成功" andIsNeedTitle:NO andShowTime:2.0];
            if (_isSecond==YES) {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
//                JobViewController *job = [[JobViewController alloc] init];
//                [self.navigationController pushViewController:job animated:NO];

            }
            
//            ClientViewController *clientVC = [[ClientViewController alloc] init];
//            [self.navigationController pushViewController:clientVC animated:NO];
//            [_tabView reloadData];
        }
        else
        {
            [_manager MakeMyselfAlert:@"修改失败" andIsNeedTitle:NO andShowTime:2.0];
        }
    }
}
-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuDropDown object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuKeXLaiFX object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   self.title = @"修改来访";
    zongDic =[NSMutableDictionary dictionaryWithCapacity:0];
    egeArr = [NSMutableArray arrayWithCapacity:0];
    egeJArr = [NSMutableArray arrayWithCapacity:0];
    dengArr = [NSMutableArray arrayWithCapacity:0];
    dengJArr = [NSMutableArray arrayWithCapacity:0];
    
    _zhuanArr = [NSMutableArray arrayWithCapacity:0];
    _adArr = [NSMutableArray arrayWithCapacity:0];
    _tuiQArr = [NSMutableArray arrayWithCapacity:0];
    _tuiFArr = [NSMutableArray arrayWithCapacity:0];
    _juArr = [NSMutableArray arrayWithCapacity:0];
    _workArr = [NSMutableArray arrayWithCapacity:0];
    _IDArr = [NSMutableArray arrayWithCapacity:0];
    _traficArr = [NSMutableArray arrayWithCapacity:0];
    _muDArr = [NSMutableArray arrayWithCapacity:0];
    _xWuArr = [NSMutableArray arrayWithCapacity:0];
    _xMianArr = [NSMutableArray arrayWithCapacity:0];
    _xHukArr = [NSMutableArray arrayWithCapacity:0];
    _oneArr = [NSMutableArray array];
    _bArr = [NSMutableArray arrayWithCapacity:0];
    _cArr = [NSMutableArray arrayWithCapacity:0];
    _dArr = [NSMutableArray arrayWithCapacity:0];
    _eArr = [NSMutableArray arrayWithCapacity:0];
    _fArr = [NSMutableArray arrayWithCapacity:0];
    _gArr = [NSMutableArray arrayWithCapacity:0];
    _hArr = [NSMutableArray arrayWithCapacity:0];
    _iArr = [NSMutableArray arrayWithCapacity:0];
    _JArr = [NSMutableArray array];
    _kArr = [NSMutableArray array];
    
    _chuanArr = [NSMutableArray array];
    _kangArr = [NSMutableArray array];
    _yinArr = [NSMutableArray array];
    _chuaFArr = [NSMutableArray array];
    _manager = [TodoHttpManager sharedManager];
    _model = [[AddMModel alloc] init];
    tfArr = [NSMutableArray arrayWithObjects:@"1",@"请输入客户姓名",@"请输入联系电话",@"1",@"请输入身份证号",@"请输入身份证地址",@"1",@"1",@"请输入客户白描",@"1",@"1",@"1",@"请输入推荐人姓名",@"请输入推荐人电话",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1", nil];
    oneArr =[ NSMutableArray arrayWithObjects:@"来访日期:",@"客户姓名:",@"联系方式:",@"客户性别:",@"身份证号码:",@"身份证地址:",@"客户等级:",@"年龄阶段:",@"客户白描:",@"广告推广:",@"推广渠道:",@"推介方式:",@"推荐人姓名:",@"推荐人电话:",@"居住区域:",@"工作区域:",@"职业身份:",@"交通工具:",@"置业目的:",@"需求物业:",@"需求面积:",@"需求户型:",@"抗性因素:",@"下次跟进日期:", nil];

    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 44;
    [self.view addSubview:_tabView];

    if (_visitChDic != nil)
    {
        _timeStr = [_visitChDic objectForKey:@"visit_time"];
       // NSLog(@"====== riqi%@",_timeStr);
        _model.xingming = [_visitChDic objectForKey:@"name"];
        _model.fangshi = [_visitChDic objectForKey:@"phone"];
        _sexString = [_visitChDic objectForKey:@"sex"];
        _model.haoma = [_visitChDic objectForKey:@"id_card"];
        _model.dizhi = [_visitChDic objectForKey:@"id_card_dz"];
        
        _keDengStr = [_visitChDic objectForKey:@"khdj"];
        _ageStr = [_visitChDic objectForKey:@"age_group"];
       // _zhuanStr = [_visitChDic objectForKey:@"caller_to_visit"];
        
        _model.baimiao = [_visitChDic objectForKey:@"remark"];
      
        _adStr = [_visitChDic objectForKey:@"ggtg"];
        _tQStr = [_visitChDic objectForKey:@"tjqd"];
        
        _tFStr = [_visitChDic objectForKey:@"tjfs"];
        _model.tuiming = [_visitChDic objectForKey:@"recommend_name"];
        _model.tuidianhua = [_visitChDic objectForKey:@"recommend_phone"];
    
        _liveStr = [_visitChDic objectForKey:@"live_area"];
        _workStr = [_visitChDic objectForKey:@"work_area"];
        _zhiStr = [_visitChDic objectForKey:@"job"];
        
        _trafficStr = [_visitChDic objectForKey:@"vehicle"];
        _zhiMStr = [_visitChDic objectForKey:@"zymd"];
        _xWStr = [_visitChDic objectForKey:@"need_property"];
        _xMianStr =[_visitChDic objectForKey:@"order_area"];
        
        _xHuStr = [_visitChDic objectForKey:@"order_type"];
      _kangYStr  = [_visitChDic objectForKey:@"doubt"];
       // NSLog(@"---_kangYStr========---%@",_kangYStr);
        _xiaStr = [_visitChDic objectForKey:@"remind_time"];
        
       phone_A = [_visitChDic objectForKey:@"phone_a"];
      phone_B = [_visitChDic objectForKey:@"phone_b"];
        NSLog(@"------其他的电话是---%@--%@",phone_A,phone_B);
        [self.seletedPhoneArry removeAllObjects];
        if (_model.fangshi.length>0) {
            [self.seletedPhoneArry addObject:_model.fangshi];
        }
        if (phone_A.length>0) {
            [self.seletedPhoneArry addObject:phone_A];
        }
        if (phone_B.length>0) {
            [self.seletedPhoneArry addObject:phone_B];
        }
        _chuaFArr = [[NSMutableArray alloc] initWithObjects:_timeStr,_model.xingming,_model.fangshi,_sexString,_model.haoma,_model.dizhi,_keDengStr,_ageStr,_model.baimiao,_adStr,_tQStr,_tFStr,_model.tuiming,_model.tuidianhua,_liveStr,_workStr,_zhiStr,_trafficStr,_zhiMStr,_xWStr,_xMianStr,_xHuStr,_kangYStr,_xiaStr, nil];
        
        [_tabView reloadData];
    }
    //第二步,通知中心,发送一条消息通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLabelText:) name:@"ChangeTitle" object:nil];
    
  //  NSLog(@"传过来的都是什么=------%@",_visitChDic);
}

- (void)showLabelText:(NSNotification *)notification
{
    //第三,实现通知中心内部的方法,并实现传值
    _chuanArr = notification.object;
    _kangYStr = [_chuanArr componentsJoinedByString:@","];
    if (_kangYStr.length>0) {
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};  //指定字号
        CGRect rect = [_kangYStr boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-8-105, MAXFLOAT)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                       NSStringDrawingUsesFontLeading attributes:dic context:nil];
        _kangYinTextHeight = rect.size.height;
        if (_kangYinTextHeight<18) {
            _kangYinTextHeight = 18;
        }
        _kangYinHeight = _kangYinTextHeight +16;
    }else{
        _kangYinTextHeight = 18;
        _kangYinHeight = 50;
    }
    [_tabView reloadData];
     NSLog(@"传过来的是什么======%@",notification.object);
    //self.notificationField.text = text;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _chuaFArr.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame= CGRectMake(20, 25,B_WIDTH-20*2, 45);
    btn.layer.cornerRadius = 6;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"保存" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn addTarget:self action:@selector(visitBaoCunBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    [self.view addSubview:view];
    return view;
}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 0||indexPath.row == 23)
    {
        
        //带日历
        NSString *CellIdentifier1 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        UserfulOneCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil)
        {
            cell = [[UserfulOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        }
        
        cell.delegate = self;
        cell.riLBtn.tag = indexPath.row;
        
        cell.firstLab.text = oneArr[indexPath.row];
       // cell.timeLab.text = _chuaFArr[indexPath.row];
        if (indexPath.row == 0)
        {
             cell.timeLab.text = _timeStr;
        }
        if (indexPath.row == 23) {
             cell.timeLab.text = _xiaStr;
        }
       
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
    if (indexPath.row ==1||indexPath.row==2||indexPath.row==4||indexPath.row==5||indexPath.row==8||indexPath.row==12||indexPath.row==13)
    {
        //TextField
        NSString *CellIdentifier2 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        UserfulTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil)
        {
            cell = [[UserfulTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
            if (indexPath.row==1) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.layer.cornerRadius = 6;
                // btn.backgroundColor = [UIColor orangeColor];
                [btn setImage:[UIImage imageNamed:@"Btn_daoRu"] forState:0];
                [btn addTarget:self action:@selector(gotoAddressBookBtnClick1:) forControlEvents:UIControlEventTouchUpInside];
                btn.tag = 1;
                [cell addSubview:btn];
                _btn1 = btn;
                _btn1.selected = NO;
                [_exitArr addObject:_btn1];
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.mas_equalTo(CGSizeMake(30, 30));
                    make.right.equalTo(cell.contentView).offset(0);
                    make.top.equalTo(cell.mas_top).offset(10);
                }];
            }
            if (indexPath.row==2) {
                cell.backgroundColor = [UIColor whiteColor];
                _threecell = cell;
                
                 [cell.oneTF removeFromSuperview];
                [cell.firstLab removeFromSuperview];
                // self.threeview.tf1.text = phone_A;
                [cell.contentView addSubview:self.threeview];
                _threeview.delegate = self;
            }
        }
        
        cell.textViewBlock = ^(){
            
            NSLog(@"123");
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
            
            //将要编辑的textfield 所在的cell  使其滚动到顶部的位置
            
            [_tabView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
            
        };
        if (indexPath.row!=2) {
            cell.oneTF.delegate = self;
            cell.oneTF.tag = indexPath.row;
            cell.firstLab.text = oneArr[indexPath.row];
            cell.oneTF.placeholder = tfArr[indexPath.row];
             [cell setheadStr:nil andDetail:nil andModel:_model];
            cell.tag = 1000+indexPath.row;
        }
       // cell.oneTF.text = _chuaFArr[indexPath.row];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (indexPath.row == 1)
        {
            
            if (_model.xingming.length>0) {
                cell.oneTF.text = _model.xingming;
            }
            

        }

        
        
        return cell;
        
    }
    if (indexPath.row == 3)
        
    {
        //男女选择
        NSString *CellIdentifier3 =@"CELL";
        UserfulThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
        if (cell == nil)
        {
            cell = [[UserfulThreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
        }
        cell.firstLab.text = oneArr[indexPath.row];
        cell.sexStr = _sexStr;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.delegate = self;
        
        return cell;
    }
    if (indexPath.row == 22)
    {
        //抗性因素
        NSString *cellID = @"CellD";
        UserfulFiveCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil)
        {
            cell = [[UserfulFiveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.oneLab.text = oneArr[indexPath.row];
        cell.delegate = self;
       
//        if (_chuanArr != nil)
//        {
//
//            _kangYStr = [_chuanArr componentsJoinedByString:@","];
//             NSLog(@"这里边么-----==== %@",_kangYStr);
//
//              [cell.yinBtn setTitle:_kangYStr forState:0];
//        }
        cell.rightHeight = _kangYinTextHeight;
        cell.rightStr = _kangYStr;
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
    else
    {
        //下拉
        NSString *CellIdentifier4 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        UserfulForeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier4];
        if (cell == nil)
        {
            cell = [[UserfulForeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4];
            cell.selectBtn.tag = indexPath.row;
            [cell.selectBtn addTarget:self action:@selector(visitSelectedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            cell.delegate = self;
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        }
        cell.firstLab.text = oneArr[indexPath.row];
        _tabString = _chuaFArr[indexPath.row];
        [cell.selectBtn setTitle:_tabString.length>0?_tabString:@"无" forState:0];
        
        
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.view endEditing:YES];
    if (indexPath.row == 22)
    {
        KangYinSuVc *kangVc = [[KangYinSuVc alloc] init];
        kangVc.chuanArr = _yinArr;
        [self.navigationController pushViewController:kangVc animated:NO];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==2) {
        if (self.seletedPhoneArry.count>=3) {
            return 150;
        }else if (self.seletedPhoneArry.count==2){
            return 100;
        }else{
            return 50;
        }
//        if (_isupdatelayout) {
//            return changeheight;
//        }else{
//            return 50;
//        }
//        return 50;
    }else if (indexPath.row==22) {
        return _kangYinHeight-14>50?_kangYinHeight-14:50;
    }
    return 50;
}
- (void)gotoAddressBookBtnClick1:(UIButton *)btn{
    [self gotoAddressBookwithtag:btn.tag];
    
}
- (void)gotoAddressBookwithtag:(NSInteger )teger{
    
    _abPeoplePickerVc = [[ABPeoplePickerNavigationController alloc] init];
    _abPeoplePickerVc.peoplePickerDelegate = self;

    [self presentViewController:_abPeoplePickerVc animated:YES completion:nil];
    
}
#pragma mark - <ABPeoplePickerNavigationControllerDelegate>
// 当用户选中某一个联系人时会执行该方法,并且选中联系人后会直接退出控制器
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    // 1.获取选中联系人的姓名
    CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    //CFStringRef abFullName = ABRecordCopyCompositeName(person);
    // (__bridge NSString *) : 将对象交给Foundation框架的引用来使用,但是内存不交给它来管理
    // (__bridge_transfer NSString *) : 将对象所有权直接交给Foundation框架的应用,并且内存也交给它来管理
    NSString *lastname = (__bridge_transfer NSString *)(lastName);
    NSString *firstname = (__bridge_transfer NSString *)(firstName);
    
    NSLog(@"%@ %@", lastname, firstname);
    if (lastname==nil&&firstName!=nil)
    {
        
        _model.xingming = [NSString stringWithFormat:@"%@",firstName];
    }
    else if (firstName==nil&&lastname!=nil) {
        
        _model.xingming = [NSString stringWithFormat:@"%@",lastName];
    }
    else
    {
        _model.xingming = [NSString stringWithFormat:@"%@%@",lastName,firstName];
    }
    //_username = lastname;
    
    // 2.获取选中联系人的电话号码
    // 2.1.获取所有的电话号码
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex phoneCount = ABMultiValueGetCount(phones);
    
    NSMutableArray *tmparray = [NSMutableArray new];
    // 2.2.遍历拿到每一个电话号码
    for (int i = 0; i < phoneCount; i++) {
        if (phoneCount<=2) {
            // 2.2.1.获取电话对应的key
            NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
            
            // 2.2.2.获取电话号码
            NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
            
            NSLog(@"phoneLabel:%@ ---- phoneValue:%@", phoneLabel, phoneValue);
            _model.fangshi = phoneValue;
            
            [tmparray addObject:phoneValue];
        }

        
    }
    //TODO：获取电话号码要做的事情
    
    _isupdatelayout = YES;
    changeheight = 150;
//    [self.threeview reloadviewframe];
    [self.seletedPhoneArry removeAllObjects];
    self.seletedPhoneArry = [NSMutableArray arrayWithArray:tmparray];
    [self.threeview reloadviewwitharray:self.seletedPhoneArry];
    [self.tabView reloadData];
    self.pphoneArray = [NSMutableArray arrayWithArray:self.seletedPhoneArry];
}
#pragma mark - 改变tableview的位置（当将要编辑的时候）
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    
    //将要编辑的textfield 所在的cell  使其滚动到中间的位置
    
    [_tabView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    return YES;
}
// type 1+2-
-(void)cellThreeViewBtnClickWithIndex:(NSInteger)index Type:(NSInteger)type{
    if (index==1) { // 1
        if (type==1) {//+
            [self.seletedPhoneArry replaceObjectAtIndex:0 withObject:_threeview.tf1.text];
            if (_threeview.tf2.text.length>0) {
                [self.seletedPhoneArry replaceObjectAtIndex:1 withObject:_threeview.tf2.text];
                [self.seletedPhoneArry addObject:@""];
            }else {
                [self.seletedPhoneArry addObject:@""];
            }
            
        }else{//-
            if (_threeview.tf3.text.length>0) {
                [self.seletedPhoneArry replaceObjectAtIndex:2 withObject:_threeview.tf3.text];
            }
            [self.seletedPhoneArry removeObjectAtIndex:0];
        }
    }else if (index==2){ // 2
        if (_threeview.tf3.text.length>0) {
            [self.seletedPhoneArry replaceObjectAtIndex:2 withObject:_threeview.tf3.text];
        }
        [self.seletedPhoneArry removeObjectAtIndex:1];

    }else{ // 3
        [self.seletedPhoneArry removeObjectAtIndex:2];

    }
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.seletedPhoneArry];
    [self.threeview reloadviewwitharray:arr];

//    [self.threeview reloadviewwitharray:arr];
    [self.tabView reloadData];
    self.pphoneArray = [NSMutableArray arrayWithArray:self.seletedPhoneArry];
}

//键盘收回触发
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //回归原处
    //    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    // NSLog(@"----000   --  %@",textField.text);
    
    if (textField.tag == 1) {
        _model.xingming = textField.text;
    }else if (textField.tag == 2){
        _model.fangshi = textField.text;
    }else if (textField.tag == 4){
        _model.haoma = textField.text;
    }else if (textField.tag == 5){
        _model.dizhi = textField.text;
    }else if (textField.tag == 8){
        _model.baimiao = textField.text;
    }else if (textField.tag == 12){
        _model.tuiming = textField.text;
    }else if(textField.tag == 13){
        _model.tuidianhua = textField.text;
    }else{
    }
    
}

#pragma  mark-- cell的代理方法
-(void)changeImageBtnClick:(UIButton *)sender
{
    
    if (sender.tag==50) {
        _sexString = @"1";//男
        
    }else{
        _sexString = @"2";//女
        
    }
}

//抗性因素
-(void)yinSutBtnClick:(UILabel *)btn
{
    
    KangYinSuVc *kangVc = [[KangYinSuVc alloc] init];
      kangVc.chuanArr = _yinArr;
    [self.navigationController pushViewController:kangVc animated:NO];
}

//日历
-(void)riLiBtnClick:(UIButton *)sender withLab:(NSString *)riLiStr
{
    if (sender.tag == 0)
    {
        _timeStr = riLiStr;
      //  NSLog(@"00000000=====%@",_timeStr);
    }
    if (sender.tag == 23)
    {
        _xiaStr = riLiStr;
       // NSLog(@"24242424242424 ---T ==%@",_xiaStr);
    }
}
#pragma mark 下拉的方法
-(void)visitSelectedBtnClick:(UIButton *)sender
{
    
    if (sender.tag == 6)
    {
        [FTPopOverMenu showForSender:sender
                       withMenuArray:dengJArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:dengArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _keDengStr =dengArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_keDengStr];

             // NSLog(@"这是 ====== 等级=====%@",_keDengStr);
             
         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 7)
    {
        [FTPopOverMenu showForSender:sender
                       withMenuArray:egeJArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:egeArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _ageStr = egeArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_ageStr];

         }
                        dismissBlock:^
         {
             
         }];
    }

    else if (sender.tag == 9)
    {
        //广告推广
        [FTPopOverMenu showForSender:sender
                       withMenuArray:self.adArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_oneArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             
             _adStr = _oneArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_adStr];
             if (![_adStr isEqualToString:@"无"] && _adStr.length != 0) {
                 _tQStr =@"";
                 [_chuaFArr replaceObjectAtIndex:10 withObject:_tQStr];
             }
             
             [_tabView reloadData];
         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 10)
    {
        //推介渠道
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_tuiQArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_bArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _tQStr =_bArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_tQStr];
             if (![_tQStr isEqualToString:@"无"] && _tQStr.length != 0) {
                 _adStr =@"";
                 [_chuaFArr replaceObjectAtIndex:9 withObject:_adStr];
             }
             [_tabView reloadData];
         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 11)
    {
        //推介方式
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_tuiFArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_cArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             
             _tFStr = _cArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_tFStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 14)
    {
        //居住区域
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_juArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_dArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             
             
             _liveStr = _dArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_liveStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 15)
    {
        //工作区域
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_workArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_eArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _workStr =_eArr[selectedIndex][@"content"] ;
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_workStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 16)
    {
        //职业身份
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_IDArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_fArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _zhiStr =_fArr[selectedIndex][@"content"] ;
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_zhiStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 17)
    {
        //交通工具
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_traficArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_gArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _trafficStr = _gArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_trafficStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 18)
    {
        //置业目的
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_muDArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_hArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _zhiMStr = _hArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_zhiMStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 19)
    {
        //需求物业
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_xWuArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_iArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             
             _xWStr = _iArr[selectedIndex][@"content"];
             // NSLog(@"这点击的his=======%@",_iArr[selectedIndex][@"content"]);
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_xWStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    
    else if (sender.tag == 20)
    {
        //需求面积
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_xMianArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_JArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _xMianStr =_JArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_xMianStr];

         }
                        dismissBlock:^
         {
             
         }];
    }
    
    
    else
    {
        //需求户型
        
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_xHukArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_kArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             _xHuStr = _kArr[selectedIndex][@"content"];
             [_chuaFArr replaceObjectAtIndex:sender.tag withObject:_xHuStr];

         }
                        dismissBlock:^
         {
             
         }];
        
        
    }

}

#pragma mark --  保存向服务器发请求
-(void)visitBaoCunBtnClick:(UIButton *)btn
{
    //NSLog(@"-------%@,%@,%@,%@",_model.xingming,_model.fangshi,_model.haoma,_model.dizhi);
//    if (!_sexString) {
//        _sexString = @"1";
//    }
    

    if (!_sexString) {
        _sexString = @"1";
    }
    NSString *phone1 = @"";
    NSString *phone2 = @"";
    NSString *phone3 = @"";

    phone1 = self.threeview.tf1.text;
    phone2 = self.threeview.tf2.text;
    phone3 = self.threeview.tf3.text;
    
    UserfulTwoCell *cell = (UserfulTwoCell *)[self.view viewWithTag:1004];//身份证
    UserfulTwoCell *cell1 = (UserfulTwoCell *)[self.view viewWithTag:1005];//身份证地址
    UserfulTwoCell *cell2 = (UserfulTwoCell *)[self.view viewWithTag:1008];//白描
    UserfulTwoCell *cell3 = (UserfulTwoCell *)[self.view viewWithTag:1012];//推荐人姓名
    UserfulTwoCell *cell4 = (UserfulTwoCell *)[self.view viewWithTag:1013];//推荐人电话
    
    _model.haoma = cell.oneTF.text;
    _model.dizhi = cell1.oneTF.text;
    _model.baimiao = cell2.oneTF.text;
    _model.tuiming = cell3.oneTF.text;
    _model.tuidianhua = cell4.oneTF.text;
//    if (self.seletedPhoneArry.count==0) {
//
//    }else if (self.seletedPhoneArry.count==1){
//        phone1 = self.seletedPhoneArry[0];
//
//    }else if (self.seletedPhoneArry.count==2){
//        phone1 = self.seletedPhoneArry[0];
//        phone2 = self.seletedPhoneArry[1];
//    }else if (self.seletedPhoneArry.count==3){
//        phone1 = self.seletedPhoneArry[0];
//        phone2 = self.seletedPhoneArry[1];
//        phone3 = self.seletedPhoneArry[2];
//
//    }
    //TODO:修改来访
    [_manager UserGMKeXLaiFangWithID:_CustomID withShouFTime:_timeStr withName:_model.xingming withPhone:phone1 withPhone_A:phone2 withPhone_B:phone3 withIDCard:_model.haoma withIdCardDZ:_model.dizhi withSex:_sexString withLevel:_keDengStr withAge:_ageStr withZhuan:nil withAD:_adStr withQuDao:_tQStr withWay:_tFStr withTName:_model.tuiming withTPhone:_model.tuidianhua withLive:_liveStr withWork:_workStr withZhiID:_zhiStr withTraffic:_trafficStr withMdDi:_zhiMStr withBaiMiao:_model.baimiao withWuYe:_xWStr withXArea:_xMianStr withHuX:_xHuStr withYinXu:_kangYStr withNextData:_xiaStr];
    
   NSLog(@"--------11111-----%@",_timeStr);
   NSLog(@"--------11111-----%@",_model.fangshi);
    NSLog(@"--------11111-----%@",_threeview.tf2.text);
    NSLog(@"--------11111-----%@",_threeview.tf3.text);
//    NSLog(@"--------11111-----%@",_IDAddStr);
//    NSLog(@"--------11111-----%@",_sexString);
//    NSLog(@"--------11111-----%@",_keDengStr);
//    NSLog(@"--------11111-----%@",_ageStr);
//    NSLog(@"--------11111-----%@",_adStr);
//    NSLog(@"--------11111-----%@",_tQStr);
//    NSLog(@"--------11111-----%@",_tFStr);
//    NSLog(@"--------11111-----%@",_model.tuiming);
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeTitle" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
 
}



@end
